<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
* 管理员数据层
*/
class Admin_model extends CI_Model
{
	public $table;
	public $primary_key;

	public function __construct()
	{
		parent::__construct();
		$this->table = $this->db->dbprefix('admin');
		$this->primary_key = 'admin_id';
	}
	/*
	* 跟据主键获取一条记录
	*/
	public function get_by_id($id, $select="*")
	{
		return (array)$this->db->query("select ".$select." from ".$this->table." where `".$this->primary_key."`='".$id."'")->row_array();
	}
	/*
	* 跟据字段获取一条记录
	*/
	public function get_by_field($field, $value, $select="*")
	{
		return (array)$this->db->query("select ".$select." from ".$this->table." where `".$field."`='".$value."'")->row_array();
	}
	/*
	* 跟据条件获取一条记录
	*/
	public function get_by_where($where, $select="*")
	{
		return (array)$this->db->select($select)->where($where)->get($this->table)->row_array();
	}
	/*
	* 列表记录集
	*/
	public function result_data($where = '', $order_by = '', $start = 0, $pagesize = 20)
	{
		$sql = "select * from ".$this->table." ".$where." ".$order_by." limit ".$start.",".$pagesize;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	/*
	* 所有记录
	*/
	public function result_all($where = '', $order_by = '', $select = "*")
	{
		$sql = "select ".$select." from ".$this->table." ".$where." ".$order_by;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	/*
	* 列表记录数
	*/
	public function result_count($where = '')
	{
		$sql = "select count(1) as total from ".$this->table." ".$where;
		$row = $this->db->query($sql)->row_array();
		return (int)$row['total'];
	}
	/*
	* 主健为索引结果数据
	*/
	public function result_maps($result)
	{
		if(empty($result)) return array();
		$data = array();
		foreach($result as $k=>$val) $data[$val[$this->primary_key]] = $val;
		return $data;
	}
	/*
	* 字段自增
	*/
	public function inc_field($id, $field)
	{
		$sql = "update ".$this->table." set `".$field."`=`".$field."`+1 where `".$this->primary_key."`='".$id."'";
		$this->db->query($sql);
		return true;
	}
	/*
	* 添加记录
	*/
	public function insert($insert)
	{
		$this->db->insert($this->table, $insert);
		return $this->db->insert_id();
	}
	/*
	* 更新数据
	*/
	public function update($id, $update)
	{
		if($id < 1) return false;
		$where = array(
			$this->primary_key => $id,
		);
		$this->db->update($this->table, $update, $where);
		return true;
	}
	/*
	* 删除数据
	*/
	public function delete($id)
	{
		if($id < 1) return false;
		$where = array(
			$this->primary_key => $id,
		);
		$this->db->delete($this->table, $where);
		return true;
	}
	
	/*
	* 检查是否登录
	*/
	
	public function is_login($jump='')
	{
		@session_start();
		if(!isset($_SESSION['admin_id']) || !isset($_SESSION['admin_auth']))
		{
			if($jump)
			{
				redirect($jump);
				exit;
			}
			return false;
		}
		
		if(empty($_SESSION['admin_auth']) || $_SESSION['admin_id'] < 1)
		{
			if($jump)
			{
				redirect($jump);
				exit;
			}
			return false;
		}
		
		$row = $this->get_by_id(intval($_SESSION['admin_id']), 'admin_auth, admin_id, admin_name, group_id');
		if($row['admin_auth'] != $_SESSION['admin_auth'])
		{
			if($jump)
			{
				redirect($jump);
				exit;
			}
			return false;
		}
		else
		{

			$this->load->model('admin_group_model');
			$this->admin_group_model->load_menu($row['group_id']);	
			return $row;
		}
	}
	
	/*
	* 取得功能导航
	*/
}